<?php
/*
Plugin Name: RCJ Annibal quote widget
Description: 
Author: hery@serasera.org
Version: 1.0
Author URI: http://hery.serasera.org
*/

// Put functions into one big function we'll call at the plugins_loaded
// action. This ensures that all required plugin functions are defined.
function annibal_quote_init() {

	// Check for the required plugin functions. This will prevent fatal
	// errors occurring when you deactivate the dynamic-sidebar plugin.
	if ( !function_exists('register_sidebar_widget') )
		return;

	// This is the function that outputs our little Google search form.
	function annibal_quote($args) {

		// $args is an array of strings that help widgets to conform to
		// the active theme: before_widget, before_title, after_widget,
		// and after_title are the array keys. Default tags: li and h2.
		extract($args);

		// Each widget can store its own options. We keep strings here.
		$options = get_option('annibal_quote');
		$title = $options['title'];
		$lang = $options['lang'];

		// These lines generate our output. Widgets can be very complex
		// but as you can see here, they can also be very, very simple.
		echo $before_widget . $before_title . $title . $after_title;
		$url_parts = parse_url(get_bloginfo('home'));
		echo '<div style="margin-top:5px;text-align:center;"><script type="text/javascript" src="http://wsdl.rcj.org/js/annibalquote.php?lang=' . $lang . '"></script></div>';
		echo $after_widget;
	}

	// This is the function that outputs the form to let the users edit
	// the widget's title. It's an optional feature that users cry for.
	function annibal_quote_control() {

		// Get our options and see if we're handling a form submission.
		$options = get_option('annibal_quote');
		if ( !is_array($options) )
			$options = array('title'=>'Daily Quote', 'apikey'=>'test', 'lang'=>'IT');
		if ( $_POST['aq-submit'] ) {

			// Remember to sanitize and format use input appropriately.
			$options['title'] = strip_tags(stripslashes($_POST['aq-title']));
			$options['apikey'] = $_POST['aq-apikey'];
			$options['lang'] = $_POST['aq-lang'];
			update_option('annibal_quote', $options);
		}

		// Be sure you format your options to be valid HTML attributes.
		$title = htmlspecialchars($options['title'], ENT_QUOTES);
		

		

		// Here is our little form segment. Notice that we don't need a
		// complete form. This will be embedded into the existing form.
		echo '<p style="text-align:right;"><label for="aq-title">Title: <input style="width: 200px;" id="aq-title" name="aq-title" type="text" value="'.$title.'" /></label></p>';
		echo '<p style="text-align:right;"><label for="aq-apikey">API Key: <input style="width: 200px;" id="aq-apikey" name="aq-apikey" type="text" value="'.$options['apikey'].'" /></label></p>';
		echo '<p style="text-align:left;"><label for="aq-lang">Language: 
			<select id="aq-lang" name="aq-lang">
				<option value="' . $options['lang'] . '">' . $options['lang'] . '</option>
				<option value="EN">English</option>
				<option value="PT">Portogese</option>
				<option value="IT">Italiano</option>
			</select>
			</label></p>';
		echo '<input type="hidden" id="aq-submit" name="aq-submit" value="1" />';
	}

	// This registers our widget so it appears with the other available
	// widgets and can be dragged and dropped into any active sidebars.
	register_sidebar_widget('AnnibalQuote', 'annibal_quote');

	// This registers our optional widget control form. Because of this
	// our widget will have a button that reveals a 300x100 pixel form.
	register_widget_control('AnnibalQuote', 'annibal_quote_control', 300, 100);
}

// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded', 'annibal_quote_init');

?>